package com.qn.utils;

import com.qn.entity.Industry;
import com.qn.entity.SubIndustry;
import com.qn.entity.location.City;
import com.qn.entity.location.Provinces;
import com.qn.entity.location.Region;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * 启信宝行业导航和地区导航解构
 *
 * @author OlO
 */
@Component
@Slf4j
public class DataAcquisitionWithQiXinBao {
    /**
     * 启信宝的行业导航中获取每个行业的关键字数据
     */
    public static List<Industry> getIndustry() {
        List<Industry> industryList = new ArrayList<>();
        try {
            Document document = Jsoup.parse(new URL("https://www.qixin.com/industry-nav/"), 2000);
            //每个行业的大类
            Elements elementsByClass = document.getElementsByClass("seo-container");
            for (int i = 0; i < elementsByClass.size(); i++) {
                Element element = elementsByClass.get(i);
                Industry industry = new Industry();
                //获取大类节点文本
                Elements select = element.select(".seo-level1");
                industry.setName(select.text());
                //获取大类节点的子类文本
                Elements subLink = element.select(".seo-level2.font-f1");
                List<SubIndustry> subIndustry = new ArrayList<>();
                for (Element element1 : subLink) {
                    SubIndustry subIndustry1 = new SubIndustry();
                    subIndustry1.setName(element1.text());
                    subIndustry1.setParentId(i);
                    subIndustry.add(subIndustry1);
                }
                industry.setSubIndustry(subIndustry);
                industryList.add(industry);
            }
        } catch (SocketTimeoutException timeoutException) {
            log.error("获取地区导航数据超时");
        } catch (IOException e) {
            log.error("获取地区导航数据时出现IO流错误");
        }
        //将获取的数据返回
        return industryList;
    }

    /**
     * 地区数据
     */
    public static List<Provinces> getLocations() {
        List<Provinces> provincesList = new ArrayList<>();
        //解析地区导航 超时设置为2秒
        try {
            Document document = Jsoup.parse(new URL("https://www.qixin.com/area-nav/"), 2000);
            //先获取最外成节点
            Elements elementsByClass = document.getElementsByClass("margin-t-2x");
            //最后一个节点没有用,所以直接删除掉
            elementsByClass.remove(elementsByClass.size() - 1);
            for (Element element : elementsByClass) {
                Provinces provinces = new Provinces();
                List<City> cities = new ArrayList<>();
                //判断省就是市的地区 比如北京  判断条件:第一个子元素的class是否为seo-container 如果是则没有三级地域
                if ("seo-container".equals(element.childNode(0).attr("class"))) {
                    //省份
                    provinces.setName(element.select(".seo-level1").text());
                    cities.add(translateChildren(element, new City()));
                } else {
                    //拥有三级领域的节点
                    //省份
                    provinces.setName(element.select(".text").text());
                    Elements elements = element.select(".seo-container");
                    //多市
                    for (Element element1 : elements) {
                        cities.add(translateChildren(element1, new City()));
                    }
                }
                provinces.setCity(cities);
                provincesList.add(provinces);
            }
        } catch (SocketTimeoutException timeoutException) {
            log.error("获取地区导航数据超时");
        } catch (IOException e) {
            log.error("获取地区导航数据时出现IO流错误");
        }
        return provincesList;
    }

    /**
     * 方法说明
     *
     * @param element 参数说明
     * @param city    参数说明
     * @return com.qn.entity.location.City
     * @description TODO
     * @author olo
     * @date 2021/10/19
     */
    private static City translateChildren(Element element, City city) {
        //市
        //将市去除
        String cityName = element.select(".seo-level1").text();
        city.setName(cityName.substring(0, cityName.indexOf('市')));
        //地区
        List<Region> regions = new ArrayList<>();
        for (Element element1 : element.select(".seo-level2")) {
            Region region = new Region();
            region.setName(element1.text());
            regions.add(region);
        }
        city.setRegion(regions);
        return city;
    }
}